package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Stat;
import com.edward.model.StatAc;
import com.edward.model.StatDb;
import com.edward.model.StatItem;


public class StatManager extends HibernateDaoSupport {

	public void addStat(Stat stat) {
		
		getHibernateTemplate().save(stat);
	}
	
	public void updateStat(Stat stat) {
		
		getHibernateTemplate().update(stat);
	}
	
	public void deleteStat(int id){
		
		getHibernateTemplate().delete(getStat(id));
	}
	
	public Stat getStat(int id){
		Stat d =(Stat)getHibernateTemplate().get(Stat.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<Stat> listStat() {
		return getHibernateTemplate().find("from Stat");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,Stat stat) {
		List <Stat>list = getHibernateTemplate().find("from Stat s order by s.bank" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Stat>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <Stat>list = getHibernateTemplate().find("from Stat" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Stat>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public boolean judge(int id){
		Stat stat = getStat(id);
		if(stat.getWay().equals("提单押汇")){
			List <StatItem>list = getHibernateTemplate().find("from StatItem i  where STAT_ID = " + id );
			if(list.size()>0){ 
				return false;
			}
		 }else if (stat.getWay().equals("退税")){
			 List <StatDb>list = getHibernateTemplate().find("from StatDb i  where STAT_ID = " + id );
				if(list.size()>0){ 
					return false;
				}
		  }else{
			  List <StatAc>list = getHibernateTemplate().find("from StatAc i  where STAT_ID = " + id );
				if(list.size()>0){ 
					return false;
				}
		  }
		return true;
		
	}
	


}



